/*
 ============================================================================
 Name		: FbTabgroup.cpp
 Author	  : ceze
 Version	 :
 Copyright   : Your copyright notice
 Description : CFbTabgroup implementation
 ============================================================================
 */
#include <EIKENV.H>
#include <stringloader.h>
#include "FbTabgroup.h"
#include "ConstDefined.hrh"
#include "flashbox4.mbg"
#include <eiklabel.h>  // for example label control
CFbTabgroup::CFbTabgroup ()
{
	// No implementation required
	m_pTabArray = NULL;
	m_activeTabIndex = -1;
	m_isDimmedTabs = EFalse;
}

CFbTabgroup::~CFbTabgroup ()
{
	if (m_pTabArray )
	{
		for (TInt i = 0; i < m_pTabArray->Count (); i++ )
		{
			delete m_pTabArray->At (i );
		}
		delete m_pTabArray;
	}
	
}

void CFbTabgroup::ConstructL (const TRect& aRect, CCoeControl *pParent )
{
	//CreateWindowL (pParent );
	SetContainerWindowL(*pParent);
	m_rect = aRect;
	//分配动态数组空间
	m_pTabArray = new (ELeave) CArrayPtrFlat<CFbTabitem> (5 );
	//载入背景图片
	//m_pBack = (CEikonEnv::Static())->CreateBitmapL(KFlashboxImagePath, EMbmFlashbox4Tabbar);

}

void CFbTabgroup::SetActiveL ()
{
	SetRect (m_rect );
	ActivateL ();
}

CFbTabgroup* CFbTabgroup::NewLC (const TRect& aRect, CCoeControl *pParent )
{
	CFbTabgroup* self = new (ELeave) CFbTabgroup ();
	CleanupStack::PushL (self );
	self->ConstructL (aRect, pParent );
	return self;
}

CFbTabgroup* CFbTabgroup::NewL (const TRect& aRect, CCoeControl *pParent )
{
	CFbTabgroup* self = CFbTabgroup::NewLC (aRect, pParent );
	CleanupStack::Pop (); // self;
	return self;
}

/**
 * From CoeControl, SizeChanged.
 * Called by framework when the view size is changed.
 */
void CFbTabgroup::SizeChanged ()
{
	DrawNow ();
	//m_pLabel->SetRect(Rect());
}

/**
 * 
 */
void CFbTabgroup::SetObserver (MFbTabObserver* pObserver )
{
	m_pTabObserver = pObserver;
}



/**
 * 通过id读取index
 */
TInt CFbTabgroup::GetIndexById (TInt aTabId )
{
	TInt indexFind = -1;
	for (TInt i = 0; i < m_pTabArray->Count (); i++ )
	{
		CFbTabitem *pTempTab = static_cast<CFbTabitem*> (m_pTabArray->At (i ) );
		if (pTempTab->TabId () == aTabId )
		{
			indexFind = i;
			break;
		}
	}
	return indexFind;
}

/**
 * 通过id激活tab项
 */
void CFbTabgroup::SetActiveTabById (TInt aTabid )
{
	//查找序号
	TInt index = GetIndexById (aTabid );
	SetActiveTabByIndexL (index );
}

/**
 * 激活的Tab id 
 */
TInt CFbTabgroup::GetActiveTabId () const
{
	if (m_activeTabIndex < 0 || m_activeTabIndex >= m_pTabArray->Count () )
		return -1;
	else
		return (static_cast<CFbTabitem*> (m_pTabArray->At (m_activeTabIndex ) ))->TabId ();
}

/**
 * 激活的Tab index
 */
TInt CFbTabgroup::GetActiveTabIndex () const
{
	return m_activeTabIndex;
}

/**
 * 获取Tab项的名称
 */
HBufC* CFbTabgroup::GetTabTitleById (TInt aTabId )
{
	TInt index = GetIndexById (aTabId );
	if (index > 0 && index < m_pTabArray->Count () )
	{
		CFbTabitem * pTab = static_cast<CFbTabitem*> (m_pTabArray->At (index ) );
		return pTab->TabTitle ();
	}
	else
	{
		return NULL;
	}
}

void CFbTabgroup::SetActiveTabByIndexL (TInt /*aTabIndex*/)
{
	
}

/**
 * 包含的tab数
 */
TInt CFbTabgroup::TabCount () const
{
	return m_pTabArray->Count ();
}

/**
 * 添加Tab项
 */
//void CFbTabgroup::AddTabL(const TInt& aTabId, const TDesC& aResFileName, const TInt& aResImageId, 
//		const TInt& aResImageMaskId, const TInt& aResBackId, const TInt & aResTextId)
//{
//	
//}

/**
 * from CCoeControl
 */
void CFbTabgroup::MakeVisible(TBool aVisible)
{
	CCoeControl::MakeVisible (aVisible );
	
	if(!m_isDimmedTabs)
	{
		for(TInt i = 0; i < m_pTabArray->Count(); i++)
		{
			m_pTabArray->At(i)->MakeVisible(aVisible);
			if(aVisible)
				m_pTabArray->At(i)->DrawNow();
		}
	}
}

void CFbTabgroup::SetTabsDimmed(TBool aDimmed)
{
	m_isDimmedTabs = aDimmed;
	
	for(TInt i = 0; i < m_pTabArray->Count(); i++)
	{
		m_pTabArray->At(i)->MakeVisible(!aDimmed);
	}	
}
